package com.azirkxs.clothes.cys.controller;

import com.azirkxs.clothes.common.vo.Result;
import com.azirkxs.clothes.cys.entity.Clothes;
import com.azirkxs.clothes.cys.entity.User;
import com.azirkxs.clothes.cys.mapper.ClothesMapper;
import com.azirkxs.clothes.cys.service.IClothesService;
import com.azirkxs.clothes.cys.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author azirkxs
 * @since 2023-04-09
 */
@RestController
@RequestMapping("/clothes")
public class ClothesController {
    @Autowired
    private IClothesService clothesService;

    @Autowired
    private IUserService iUserService;
    private ClothesMapper clothesMapper;
    //查询所有衣服
    @GetMapping("/all")
    public Result<Map<String,Object>> getAllClothes(@RequestParam(value = "pageNo") Long pageNo,
                                               @RequestParam(value = "pageSize") Long pageSize){

        LambdaQueryWrapper<Clothes> wrapper = new LambdaQueryWrapper<>();
        Page<Clothes> page = new Page<>(pageNo,pageSize);
        clothesService.page(page,wrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);
    }
    // 个性推荐分页
    @GetMapping("/personal")
    public Result<Map<String,Object>> personal(@RequestParam(value = "token") String token,
                                               @RequestParam(value = "pageNo") int pageNo,
                                               @RequestParam(value = "pageSize") int pageSize){
        // 自己获取的数据
        Map<String, Object> personalReocommend = iUserService.getPersonalReocommend(token);
        List<Clothes> clothesList = (List<Clothes>) personalReocommend.get("personal");

        int startIndex = (int) ((pageNo - 1) * pageSize); // 计算起始位置
        int endIndex = Math.min(startIndex + pageSize, clothesList.size()); // 计算结束位置

        List<Clothes> pageContent = clothesList.subList(startIndex, endIndex); // 当前页的数据列表

        int totalPages = (int) Math.ceil((double) clothesList.size() / pageSize); // 总页数
        int currentPage = pageNo; // 当前页码
        long totalElements = clothesList.size(); // 总条数

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("personal", pageContent);
        resultMap.put("currentPage", currentPage);
        resultMap.put("totalPages", totalPages);
        resultMap.put("totalElements",totalElements);

        return Result.success(resultMap);
    }

    //分类查询
    @PostMapping("/selectByType")
    public Result<Map<String,Object>> getClothesByType(@RequestParam(value = "type") String type,
                                                  @RequestParam(value = "pageNo") Long pageNo,
                                                  @RequestParam(value = "pageSize") Long pageSize){

        LambdaQueryWrapper<Clothes> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Clothes::getType,type);

        Page<Clothes> page = new Page<>(pageNo,pageSize);
        clothesService.page(page,wrapper);
        Map<String,Object> data = new HashMap<>();
        data.put("total",page.getTotal());
        data.put("rows",page.getRecords());

        return Result.success(data);



    }


}
